package com.xdog.web3.controller.indexprice;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.web3.domain.indexprice.TokenHistoricalPriceQuery;
import com.xdog.web3.domain.indexprice.TokenIndexPriceRequest;
import com.xdog.web3.exception.Web3ApiException;
import com.xdog.web3.service.IIndexPriceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 综合价格控制器
 */
@Tag(name = "综合价格", description = "综合价格相关接口")
@RestController
@RequestMapping("/web3/market/index-price")
public class IndexPriceController {

    @Resource
    private IIndexPriceService indexPriceService;

    /**
     * 批量查询代币的综合价格
     */
    @Operation(summary = "获取代币综合价格", description = "批量查询代币的综合价格，每次最多可以批量查询 100 个代币的综合价格")
    @PostMapping("/current")
    @SaIgnore
    public R<JSONObject> getTokenIndexPrice(@RequestBody List<TokenIndexPriceRequest> requests) {
        try {
            JSONObject result = indexPriceService.getTokenIndexPrice(requests);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币综合价格失败: " + e.getMessage());
        }
    }

    /**
     * 查询某个代币的历史综合价格
     */
    @Operation(summary = "获取代币历史综合价格", description = "查询某个代币的历史综合价格")
    @GetMapping("/historical")
    @SaIgnore
    public R<JSONObject> getTokenHistoricalPrice(TokenHistoricalPriceQuery query) {
        try {
            JSONObject result = indexPriceService.getTokenHistoricalPrice(query);
            return R.ok(result);
        } catch (Web3ApiException e) {
            return R.fail(e.getHttpStatus(), "API错误: " + e.getMessage());
        } catch (Exception e) {
            return R.fail("获取代币历史综合价格失败: " + e.getMessage());
        }
    }
}
